/*
 * @Descripttion:菜品分类报表相关js
 * @version:
 * @Author: 森哥
 * @Date: 2022-11-08 15:57:27
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2024-10-06 13:28:17
 */

import FoodCategorySummaryCard from '../components/FoodCategorySummaryCard.vue'
import { getCategoryList, foodCategory, categoryReport } from '@/api/foodCatgorySummary'
import { getNowDateAndTime } from '@/utils/index'
export default {
  components: {
    FoodCategorySummaryCard
  },
  data() {
    return {
      // 分页筛选对应的分类数据
      pagingList: [],
      // 子组件渲染的菜品分类列表
      dishesList: {},
      dateRangeList: [],
      // 查询参数
      searchParam: {
        desk_category_list: [],
        food_category_list: []
      },
      tableCategoryIndex: -1, // 桌台分类索引
      foodCategoryIndex: -1, // 菜品分类索引
      //  桌台列表
      list: [],
      timeList: {},
      // 桌台分类列表
      tableCategoryList: [],
      // 菜品分类列表
      foodCategoryList: []
    }
  },
  mounted() {
    const { currentNowDate } = getNowDateAndTime()
    this.dateRangeList = [currentNowDate + ' ', currentNowDate + ' ']
    let promise = []
    promise.push(this.getCategoryList())
    promise.push(
      this.foodCategory({
        parent_id: 0
      })
    )
    Promise.all(promise).then(() => {
      this.categoryReport()
    })
  },
  methods: {
    // 点击激活桌台分类样式
    handleActionTable(index) {
      if (index === -1) {
        // this.tableCategoryIndex = index
        this.searchParam.desk_category_list = []
      } else {
        let isExist = this.searchParam.desk_category_list.find(
          item => item === this.tableCategoryList[index].id
        )
        if (!isExist) {
          this.searchParam.desk_category_list.push(this.tableCategoryList[index].id)
        } else {
          this.searchParam.desk_category_list = this.searchParam.desk_category_list.filter(
            item => item !== this.tableCategoryList[index].id
          )
        }
      }
      this.categoryReport()
    },
    // 点击激活菜品分类显示样式
    handleActionFood(index) {
      if (index === -1) {
        this.searchParam.food_category_list = []
      } else {
        let isExist = this.searchParam.food_category_list.find(
          item => item === this.foodCategoryList[index].id
        )
        if (!isExist) {
          this.searchParam.food_category_list.push(this.foodCategoryList[index].id)
        } else {
          this.searchParam.food_category_list = this.searchParam.food_category_list.filter(
            item => item !== this.foodCategoryList[index].id
          )
        }
      }
      console.log(this.searchParam.food_category_list)
      this.categoryReport()
    },
    getCategoryList() {
      return getCategoryList().then(res => {
        this.tableCategoryList = [...res.data]
        // this.searchParam.desk_category_list = [this.tableCategoryList[0].id]
      })
    },
    foodCategory(data) {
      return foodCategory(data).then(res => {
        this.foodCategoryList = [...res.data]
        // this.searchParam.food_category_list = [this.foodCategoryList[0].id]
      })
    },
    categoryReport() {
      let start_time = ''
      let end_time = ''
      if (this.dateRangeList) {
        start_time = this.dateRangeList[0] + '00:00:00'
        end_time = this.dateRangeList[1] + '23:59:59'
      }
      categoryReport({
        search_name: this.searchParam.search_name,
        start_time: start_time,
        end_time: end_time,
        desk_category_list: this.searchParam.desk_category_list,
        food_category_list: this.searchParam.food_category_list,
        print: '0'
      }).then(res => {
        this.dishesList = res.data.detail
        console.log(this.dishesList)
      })
    },
    handleAction() {
      this.$router.go(-1)
    },
    printTicket() {
      let start_time = ''
      let end_time = ''
      if (this.dateRangeList) {
        start_time = this.dateRangeList[0] + '00:00:00'
        end_time = this.dateRangeList[1] + '23:59:59'
      }
      categoryReport({
        start_time: start_time,
        end_time: end_time,
        desk_category_list: this.searchParam.desk_category_list,
        food_category_list: this.searchParam.food_category_list,
        print: '1'
      }).then(res => {
        this.$message.success('推送打印成功')
      })
    }
  }
}
